package org.jeecg.modules.qiwei.shenpi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.QiWeiUtil;
import org.jeecg.modules.oa.entity.FormAttLeaveApplyEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.Date;

@Slf4j
@Component
public class QingJiaShenPi {
    @Autowired
    private QiWeiUtil qiWeiUtil;

    private String spNo ;
    private String spName;
    private Integer spStatus ;//审批状态 1审批中 2已同意 3已拒绝 4已撤销 5通过后撤销 6已删除
    private String templateId ; //模板
    private String applyTime ;//申请时间
    private String userid ; //申请人
    private String applyGongHao ; //申请人工号
    private String partyid; //部门ID
    private String nodeType ; //审批节点类型 1审批节点 2审批人 3抄送人
    private String apvRel ; //审批关系 1或签 2会签
    public void handleApprovalDetail(JSONObject approvalDetail) {
        Integer errcode = approvalDetail.getInteger("errcode");
        if (errcode !=0){
            throw new RuntimeException("获取审批详情失败："+approvalDetail.getString("errmsg"));
        }

        FormAttLeaveApplyEntity formAttLeaveApplyEntity = new FormAttLeaveApplyEntity();



        JSONObject info = approvalDetail.getJSONObject("info");
        spNo = info.getString("sp_no"); //审批单号
        log.info("审批单号:"+spNo);

        spName = info.getString("sp_name"); //审批名称
        log.info("审批名称:"+spName);

        spStatus = info.getInteger("sp_status"); //审批状态 1审批中 2已同意 3已拒绝 4已撤销 5通过后撤销 6已删除
        log.info("审批状态:"+spStatus);

        templateId = info.getString("template_id"); //模板
        log.info("模板:"+templateId);

        applyTime = info.getString("apply_time"); //申请时间
         log.info("申请时间的时间戳:"+applyTime);
      //  Timestamp timestamp = DateUtils.getTimestamp(applyTime);
        Date dateApplyTime = DateUtils.getDate(Long.parseLong(applyTime) *1000);
        log.info("申请时间:"+ DateUtils.formatDate(dateApplyTime,"yyyy-MM-dd HH:mm:ss") );

        //申请人
        JSONObject applyer = info.getJSONObject("applyer");
        userid = applyer.getString("userid");//申请人
        log.info("申请人:"+userid);
        applyGongHao = qiWeiUtil.GongHao(userid); //申请人工号
        log.info("申请人工号:"+applyGongHao);
        partyid = applyer.getString("partyid");//部门ID
        log.info("部门ID:"+partyid);

        //审批流程
        JSONObject processList = info.getJSONObject("process_list");
        JSONArray nodeList = processList.getJSONArray("node_list");
        if(nodeList.size()> 0){
            JSONObject node = nodeList.getJSONObject(0);
            nodeType = node.getString("node_type"); //审批节点类型 1审批节点 2审批人 3抄送人
            apvRel = node.getString("apv_rel"); //审批关系 1或签 2会签

            JSONArray sub_node_list = node.getJSONArray("sub_node_list");
            for(int i=0;i<sub_node_list.size();i++){
                JSONObject sub_node = sub_node_list.getJSONObject(i);
                String nodeUserid = sub_node.getString("userid"); //审批人
                String nodeGongHao = qiWeiUtil.GongHao(nodeUserid);
                String speech = sub_node.getString("speech"); //审批意见

                String sp_yj = sub_node.getString("sp_yj"); //审批意见 1同意 2拒绝

                String sptime = sub_node.getString("sptime");  //审批时间
                JSONArray media_ids = sub_node.getJSONArray("media_ids"); //审批附件
            }

        }

        //备注
        String comments = info.getString("comments");
        log.info("备注:"+comments);
        //JSONArray comments = info.getJSONArray("comments");
//        for (int i=0;i<comments.size();i++){
//            JSONObject comment = comments.getJSONObject(i);
//
//            String commentId = comment.getString("commentid");
//            String commentContent = comment.getString("commentcontent");
//            String commentTime = comment.getString("commenttime");
//            JSONObject commentUserInfo = comment.getJSONObject("commentUserInfo");
//            String commentUserid = commentUserInfo.getString("userid");
//            String commentGongHao = qiWeiUtil.GongHao(commentUserid);
//            String commentMediaId = comment.getString("media_id"); // 备注附件数组
//
//        }

        //抄送人
        JSONArray notifyer = info.getJSONArray("notifyer");
        for (int i=0;i<notifyer.size();i++){
            JSONObject notify = notifyer.getJSONObject(i);
            String notifyUserid = notify.getString("userid");
            log.info("抄送人:"+notifyUserid);
            String notifyGongHao = qiWeiUtil.GongHao(notifyUserid);
            log.info("抄送人工号:"+notifyGongHao);
        }


        //申请数据
        JSONObject applyData = info.getJSONObject("apply_data");
        JSONArray contents = applyData.getJSONArray("contents");

    //FormName,  Hours, SignSerialNo, Remark, Url, Url2,
        // Url3, CreatedBy, CreatedDate, UpdatedBy, UpdatedDate, IsActive
        formAttLeaveApplyEntity.setUsercode(applyGongHao); //申请人
        String jiaType = "事假";

        formAttLeaveApplyEntity.setHandover(""); //交接人

        for (int i = 0; i < contents.size(); i++) {
            JSONObject content = contents.getJSONObject(i);
            JSONArray title = content.getJSONArray("title");
            String titleValue = title.getJSONObject(0).getString("text");

            if (titleValue.equals("请假类型")) {
                JSONObject contentValue = content.getJSONObject("value");
                JSONObject selector = contentValue.getJSONObject("selector");
                JSONArray options = selector.getJSONArray("options");
                JSONObject firstOption =options.getJSONObject(0) ;
                JSONArray value = firstOption.getJSONArray("value");
                JSONObject valueJSONObject = value.getJSONObject(0);
                jiaType = valueJSONObject.getString("text");
                log.info("请假类型:" + jiaType);
            } else if (titleValue.equals("开始时间")) {
                JSONObject contentValue = content.getJSONObject("value");
                JSONObject date = contentValue.getJSONObject("date");
                String s_timestamp = date.getString("s_timestamp");
                Date dateS_timestamp = DateUtils.getDate(Long.parseLong(s_timestamp)*1000);
                log.info("开始时间:" +  DateUtils.formatDate(dateS_timestamp, "yyyy-MM-dd HH:mm:ss"));

                formAttLeaveApplyEntity.setStartdate(DateUtils.formatDate(dateS_timestamp, "yyyy-MM-dd"));
                formAttLeaveApplyEntity.setStarttime(DateUtils.formatDate(dateS_timestamp, "HH:mm"));

            } else if (titleValue.equals("结束时间")) {
                JSONObject contentValue = content.getJSONObject("value");
                JSONObject date = contentValue.getJSONObject("date");
                String s_timestamp = date.getString("s_timestamp");
                Date dateS_timestamp = DateUtils.getDate(Long.valueOf(s_timestamp) *1000);
                log.info("结束时间:" + DateUtils.formatDate( dateS_timestamp, "yyyy-MM-dd HH:mm:ss"));
                formAttLeaveApplyEntity.setEnddate(DateUtils.formatDate(dateS_timestamp, "yyyy-MM-dd"));
                formAttLeaveApplyEntity.setEndtime(DateUtils.formatDate(dateS_timestamp, "HH:mm"));

            } else if (titleValue.equals("请假事由")) {
                JSONObject contentValue = content.getJSONObject("value");
                String text = contentValue.getString("text");
                log.info("请假事由:" + text);

                formAttLeaveApplyEntity.setLeavereason(text); //请假事由

            } else if (titleValue.equals("说明附件")) {
                JSONObject contentValue = content.getJSONObject("value");
                String files = contentValue.getString("files");
                log.info("说明附件:" + files);
            }

        }

        formAttLeaveApplyEntity.setFormname("");
        formAttLeaveApplyEntity.setCategory(jiaType); //请假类型

    }


}
